package cn.pugle.oj.leetcode;

import cn.pugle.oj.catalog.LinkedProblem;

/**
 * 要求 On时间 O1空间
 * 错误写法, 翻转链表后原来的指针都特么没了
 *
 * @author tzp
 * @since 2020/10/15
 */
public class LC234 implements LinkedProblem {
    public boolean isPalindrome(ListNode head) {
        if (head == null || head.next == null) return true;
        ListNode reverse = reverseList(head);
        while (reverse != null) {
            if (reverse.val != head.val) return false;
            reverse = reverse.next;
            head = head.next;
        }
        return true;
    }

    public ListNode reverseList(ListNode head) {
        ListNode cur = head, newHead = null;
        while (cur != null) {
            ListNode tmp = cur.next;
            cur.next = newHead;
            newHead = cur;
            cur = tmp;
        }
        return newHead;
    }

    public static void main(String[] args) {
        ListNode a = ListNode.arrayToListNode(new int[]{1, 2, 3, 2, 1});
        System.out.println(new LC234().isPalindrome(a));
    }
}
